<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

include('../includes/db_connect.php');
include('../functions/fonctions.php');
//OPERATIONS DE CREATION D'UNE DIVISION
//On verifie si le formulaire a ete envoye
    if(isset($_POST['submit']))
    {   
        if(get_magic_quotes_gpc())
        {
            $category = stripslashes(htmlspecialchars($_POST['category']));
            $materiel = stripslashes(htmlspecialchars($_POST['materiel']));
            $acquisition = stripslashes(htmlspecialchars($_POST['acquisition']));
            $amortissement = stripslashes(htmlspecialchars($_POST['amortissement']));
            $etat = stripslashes(htmlspecialchars($_POST['etat']));
        } else {
            $category = mysql_real_escape_string(htmlspecialchars($_POST['category']));
            $materiel = mysql_real_escape_string(htmlspecialchars($_POST['materiel']));
            $acquisition = stripslashes(htmlspecialchars($_POST['acquisition']));
            $amortissement = stripslashes(htmlspecialchars($_POST['amortissement']));
            $etat = stripslashes(htmlspecialchars($_POST['etat']));
        }
        if ($category!=NULL && $materiel!=NULL && $acquisition!=NULL && $amortissement!=NULL && $etat!=NULL){
            
                $result = mysql_insert('materiel', 
                        array( 
                            'materiel_label' => $materiel, 
                            'category_id'=>$category,
                            'date_acquisition'=>$acquisition,
                            'taux_amortissement'=>$amortissement,
                            'etat'=>$etat
                        )
                    );
                $form = false;
                $_SESSION['success_message'] = '<strong>Alerte!!!</strong> Le matériel a été ajouté avec succès.<br />';
                unset($materiel);
                unset($category);
                unset($acquisition);
                unset($amortissement);
                unset($etat);            
        } else {
            //Sinon, on indique que le champ ne doit pas être vide
       
            $form = true;
            $_SESSION['error_message'] = '<strong>Désolé!!!</strong> Aucun des champs ne doit être vide.';
        }
    } 
//FIN DES OPERATIONS DE CREATION D'UNE DIRECTION
$categories = mysql_query('SELECT * FROM category WHERE category_deleted="0"');    
$materiels = mysql_query('
        SELECT x.*, c.* 
        FROM materiel x 
            LEFT JOIN category c  ON x.category_id = c.category_id 
        WHERE x.materiel_deleted="0"'); 
//LE TITRE DE LA PAGE
$titre = 'MATERIEL';
//MENU ACTIF
$menu = 'patrimony';
//SOUS MENU ACTIF
$sous_menu = 'materiel';
//LE TITRE DE L'OPERATION
$page_header_title = array('h4'=>'Materiel','small'=>'Le materiel de votre patrimoine');
//LE HAUT DE LA PAGE
include('../layout/header.php');
?>

      <!-- Le contenu de notre page -->

      <div class="row">
        <div class="col-md-6">
            <div class="panel panel-success">
                <div class="panel-heading">
                    <h3 class="panel-title">Formulaire de creation d'un materiel</h3>
                </div>
                <div class="panel-body">
                     <!--Début du formulaire-->
                    <form class="form-horizontal" role="form" method="post" action="">
                        <div class="form-group">
                            <label for="category" class="col-sm-5 control-label">Categorie</label>
                            <div class="col-sm-7">
                                <select class="form-control" id="category" name="category">
                                    <?php if (@mysql_num_rows($categories) == 0){?>
                                        <option value="">Ajoutez du materiel</option>
                                    <?php } else { ?>
                                        <option value="">--Selection de la categorie--</option>
                                    <?php while ($categorie = mysql_fetch_assoc($categories)) {?>
                                        <option <?php echo isset($category) ? (($category == $categorie['category_id']) ? 'selected' : '' ) : '';?> value="<?php echo $categorie['category_id']; ?>"><?php echo $categorie['category_label']; ?></option>
                                        <?php } ?>                                        
                                    <?php  } ?>
                                </select> 
                            </div>
                        </div>
                        <div class="form-group">
                          <label for="materiel" class="col-sm-5 control-label">Dénomination</label>
                          <div class="col-sm-7">
                            <input type="text" class="form-control" id="division" name="materiel" placeholder="Par ex. Ordinateur" autocomplete="off" value="<?php echo isset($materiel) ? $materiel : ''; ?>">
                          </div>
                        </div>
                        <div class="form-group">
                          <label for="acquisition" class="col-sm-5 control-label">Date d'acquisition</label>
                          <div class="col-sm-7">
                              <input type="date" class="form-control" id="acquisition" name="acquisition"  autocomplete="off" value="<?php echo isset($acquisition) ? $acquisition : ''; ?>">  
                          </div>
                        </div>
                        <div class="form-group">
                          <label for="amortissement" class="col-sm-5 control-label">Taux d'amortissement en pourcentage</label>
                          <div class="col-sm-7">
                              <input type="number" min="0" max="100" class="form-control" id="amortissement" name="amortissement"  autocomplete="off" value="<?php echo isset($amortissement) ? $amortissement : ''; ?>">  
                          </div>
                        </div>
                        <div class="form-group">
                          <label for="etat" class="col-sm-5 control-label">Etat</label>
                          <div class="col-sm-7">
                              <select class="form-control" id="etat" name="etat">
                                  <option value="1">R.A.S</option>
                                  <option value="2">Endomagé</option>
                                  </select>
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="col-sm-offset-5 col-sm-7">
                            <button type="submit" class="btn btn-default" name="submit" value="1"><i class="glyphicon glyphicon-floppy-saved"></i> Enregistrer</button>
                          </div>
                        </div>
                        
                      </form>
                    <!--Fin du formulaire-->
                </div>
                <div class="panel-footer"></div>
            </div>
            </div>
            <div class="col-md-6">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <h3 class="panel-title">Liste du materiel</h3>
                    </div>
                    <div class="panel-body">
                        <table class="table table-hover">
                          <thead>
                            <tr>
                              <th>#</th>
                              <th>Categorie</th>
                              <th>Dénomination</th>
                              <th>Actions</th>
                            </tr>
                          </thead>
                          <tbody>
                            <?php if(@mysql_num_rows($materiels)>0){
                                while ($materiel= mysql_fetch_array($materiels)){ ?>
                                    <tr>
                                      <td><?php echo $materiel['materiel_id'] ?></td>
                                      <td><?php echo $materiel['category_label'] ?></td>
                                      <td><?php echo $materiel['materiel_label'] ?></td>
                                      <td>
                                            <a type="button" class="btn btn-default btn-xs" data-widget="delete" data-toggle="tooltip" data-original-title="Delete" href="<?php ?>"><i class="glyphicon glyphicon-trash"></i></a>
                                            <a type="button" class="btn btn-default btn-xs" href="<?php ?>"><i class="glyphicon glyphicon-edit"></i></a>
                                      </td>
                                    </tr>
                            <?php    }
                            } else { ?>
                                <tr>
                                  <td colspan="4">Pas de materiel pour l'instant</td>
                                </tr>
                            <?php }?>                       

                          </tbody>
                        </table>
                    </div>
                    <div class="panel-footer">
                        <ul class="pagination pagination-sm">
                            <li><a href="#">«</a></li>
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">»</a></li>
                          </ul>
                    </div>
                </div>
            </div>            
        </div>
    </div>
          
      <!-- La fin du contenu de notre page -->
<?php 
    include('../layout/footer.php');
?>
    <!--Ecrire les scripts jQuery ici-->
    <script>
        $('#acquisition').datepicker({
            format: 'dd/mm/yyyy'
        })
    </script>
    

</body>
</html>

